// 获取应用实例
const app = getApp()
const api = app.globalData.api

Page({
    // 默认数据
    data: {
        warehouseOption: [],
        now: '',
        state: [],
        result: [],
        page: 1,
        pageAll: 0,
        where: '',
        sortShow: false,
        sortArray: [],
        searchShow: false,
        searchArray: {}
    },
    // 获取仓库列表
    search: async function () {
        let result = await api.curlGet('?cla=machiningInOrder&fun=search')
        if (result.code === 0) {
            api.showToast(res.warn)
        } else {
            const res = result.data
            this.setData({
                sortArray: res.orderBy,
                searchArray: res.search,
            })
        }
    },
    // 列表数据
    list: async function (param = {}, page = 1) {
        const url = '?cla=machiningInOrder&fun=list&page=' + page
        // 记住搜索
        const data = this.data.where === '' ? {} : api.jsonToArray(this.data.where)
        // 合并搜索
        let params = Object.assign(data, param)
        let res = await api.curlPost(url, params)
        if (res.code === 1) {
            const page = res.data.page
            this.setData({
                result: res.data.result,
                page: page.pageNow,
                pageAll: page.pageAll,
                where: api.arrayToJson(params)
            })
        } else {
            api.showToast(res.warn)
        }
    },
    onSearch: async function (e) {
        if (e.detail) {
            this.list({orderNo: e.detail})
        }
    },
    onDetail: function (obj) {
        let data = obj.currentTarget.dataset
        if (data.id) {
            wx.navigateTo({
                url: 'detail?id=' + data.id,
            })
        }
    },
    main: async function () {
        await this.search()
        await this.list()
    },
    onLoad: async function (options) {
        this.main()
    },
    addInStock: function (options) {
        wx.navigateTo({
            url: 'add',
        })
    },
    // 上拉加载
    onReachBottom() {
        if (!this.loading && this.data.page < this.data.pageAll) {
            const page = parseInt(this.data.page),
                param = {}
            this.list(param, page + 1)
        }
    },
    // 下拉刷新
    onPullDownRefresh() {
        if (!this.loading) {
            this.list({}).then(() => {
                // 处理完成后，终止下拉刷新
                wx.stopPullDownRefresh()
            })
        }
    },
    showHide(obj, bool = true) {
        const field = obj.currentTarget.dataset.target
        if (field) {
            this.setData({
                [field]: bool
            })
        }
    },
    // 排序
    sortFun(obj) {
        let array = obj.detail,
            that = this
        if (array.act === '确定') {
            that.list({
                orderBy: array.val
            })
        }
        that.setData({
            sortShow: false
        })
    },
    // 筛选
    searchFun(obj) {
        let array = obj.detail,
            that = this

        if (array.act === '确定') {
            that.list(array.param)
        }
        that.setData({
            searchShow: false
        })
    }
});